package ui.tp3.TP3Wicket;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.IAjaxCallDecorator;
import org.apache.wicket.ajax.form.AjaxFormSubmitBehavior;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.ImageButton;

import org.apache.wicket.model.IModel;
import org.apache.wicket.request.resource.ResourceReference;
import org.apache.wicket.util.string.AppendingStringBuffer;
import org.apache.wicket.util.value.ValueMap;
import org.eclipse.jetty.plus.jndi.Resource;

/**
  * An ajax submit image button that will degrade to a normal request  
if ajax is not available or
  * javascript is disabled.
  *
  * @author Jeremy Thomerson (jthomerson)
  * @author Alastair Maw
  */
public abstract class AjaxFallbackImageButton extends ImageButton
{
   private static final long serialVersionUID = 1L;

   private Form<?> mForm;


   /**
    * Construct, see {@link ImageButton#ImageButton(String,  
ResourceReference)}.
    *
    * @param id
    * @param resourceReference
    * @param form
    */
   public AjaxFallbackImageButton(String id, ResourceReference  
resourceReference, Form<?> form)
   {
     super(id, resourceReference);
     initialize(form);
   }

   /**
    * Construct, see {@link ImageButton#ImageButton(String,  
ResourceReference, ValueMap)}.
    *
    * @param id
    * @param resourceReference
    * @param resourceParameters
    * @param form
    */
   public AjaxFallbackImageButton(String id, ResourceReference  
resourceReference, ValueMap resourceParameters,
       Form<?> form)
   {
     super(id, resourceReference);
     initialize(form);
   }


   /**
    * Construct, see {@link ImageButton#ImageButton(String, IModel)}.
    *
    * @param id
    * @param model
    * @param form
    */
   public AjaxFallbackImageButton(String id, IModel<String> model,  
Form<?> form)
   {
     super(id, model);
     initialize(form);
   }

   /**
    * Construct, see {@link ImageButton#ImageButton(String, String)}.
    *
    * @param id
    * @param resourceReference
    * @param resourceParameters
    * @param form
    */
   public AjaxFallbackImageButton(String id, String string, Form<?>  
form)
   {
     super(id, string);
     initialize(form);
   }

   private void initialize(Form<?> form)
   {
     mForm = form;

     add(new AjaxFormSubmitBehavior(form, "onclick")
     {
       private static final long serialVersionUID = 1L;

       @Override
       protected void onSubmit(AjaxRequestTarget target)
       {
         AjaxFallbackImageButton.this.onSubmit(target,  
AjaxFallbackImageButton.this.getForm());
       }

       @Override
       protected void onError(AjaxRequestTarget target)
       {
         AjaxFallbackImageButton.this.onError(target,  
AjaxFallbackImageButton.this.getForm());
       }

       @Override
       protected CharSequence getEventHandler()
       {
         return new  
AppendingStringBuffer(super.getEventHandler()).append("; return  false;");
       }

       @Override
       protected IAjaxCallDecorator getAjaxCallDecorator()
       {
         return AjaxFallbackImageButton.this.getAjaxCallDecorator();
       }
     });
   }

   /**
    * Listener method invoked on form submit with errors
    *
    * @param target
    * @param form
    *
    * TODO 1.3: Make abstract to be consistent with onsubmit()
    */
   protected void onError(AjaxRequestTarget target, Form<?> form)
   {
     // created to override
   }


   /**
    *
    * @see org.apache.wicket.markup.html.form.Button#getForm()
    */
   @Override
   public Form<?> getForm()
   {
     return mForm == null ? super.getForm() : mForm;
   }

   /**
    * Callback for the onClick event. If ajax failed and this event  
was generated via a normal
    * submission, the target argument will be null
    *
    * @param target
    *          ajax target if this linked was invoked using ajax, null  
otherwise
    * @param form
    */
   protected abstract void onSubmit(final AjaxRequestTarget target,  
final Form<?> form);

   /**
    *
    * @return call decorator to use or null if none
    */
   protected IAjaxCallDecorator getAjaxCallDecorator()
   {
     return null;
   }

   /**
    * Helper methods that both checks whether the link is enabled and  
whether the action ENABLE is
    * allowed.
    *
    * @return whether the link should be rendered as enabled
    */
   protected final boolean isButtonEnabled()
   {
     return isEnabled() && isEnableAllowed();
   }
} 
